import itertools
import json
import os
import re

import lxml
import lxml.cssselect
import requests


def stringify(node):
    parts = [node.text.strip()]
    for c in node.getchildren():
        parts.append(stringify(c))
    parts.append(node.tail.strip())
    text = ' '.join(filter(None, parts))
    return re.sub(r'\s+', ' ', text).strip()


filepath = '/tmp/redis-commands.html'
if not os.path.exists(filepath):
    resp = requests.get('https://redis.io/commands')
    with open(filepath, 'wb') as f:
        f.write(resp.content)

data = {}
with open(filepath) as f:
    content = f.read()
parser = lxml.etree.HTMLPullParser(encoding='utf-8')
html = lxml.etree.HTML(content, parser)
for el_op in html.cssselect('select.command-reference-filter > option'):
    value = el_op.attrib['value'].strip()
    if not value:
        continue
    data.setdefault(value, {
        'name': el_op.text.strip(),
        'value': value,
        'children': {},
    })
for el_li in html.cssselect('#commands > div > ul > li'):
    print([el_li,  # lxml.etree.tostring(el_li),
           stringify(el_li), el_li.attrib])
    el_command = el_li.cssselect('span.command')[0]
    el_summary = el_li.cssselect('span.summary')[0]
    command = stringify(el_command)
    summary = stringify(el_summary)
    group = el_li.attrib['data-group']
    name = el_li.attrib['data-name']
    print([group, name, command, summary])
    data.setdefault(group, {'name': group, 'value': group, 'children': {}})
    data[group]['children'][name] = {
        'command': command,
        'summary': summary,
    }

print(json.dumps(data, indent=4, ensure_ascii=False))
print(list(data.keys()))

f = open('/tmp/redis-commands.md', 'w')
print('# Redis 命令大全', file=f)
for g in data.values():
    print(file=f)
    print(f'## {g["name"]}', file=f)
    print(file=f)
    for c in g['children'].values():
        print(f'- `{c["command"].lower()}`  ', file=f)
        print(f'  {c["summary"]}', file=f)
